<?php
namespace App\Library\System\Model;

use App\Library\System\Model;
use App\Library\System\Config;
use Zend\Http\Header\UserAgent;

class AppModel extends Model
{
    private static $static_app;

    public static function getInstance()
    {
        if (!self::$static_app) {
            self::$static_app = new AppModel();
        }

        return self::$static_app;
    }

    public function loginUser($email, $password)
    {

        return $this->more($this->select()
                ->from('user')
                ->columns(array(
                    'id',
                    'email',
                    'name',
                    'password'
                ))
                ->where(array(
                    'email'    => $email,
                    'password' => $password
                ))
        );
    }

    public function getCurrentLanguage()
    {
        $app_lang = $this->session('app_lang');

        if (empty($app_lang)) {
            $app_lang = Config::getInstance()->language->default;
        }

        $language = explode('-', $app_lang); // [en-US]
        $language_alias = $language[0];

        return $this->one($this->select()
                ->from('language')
                ->columns(array(
                    'name'
                ))
                ->where(array(
                     'alias' => $language_alias
                ))
        );
    }

    public function saveRegisterForm($name, $email, $hash)
    {
        $this->session('register_name', $name);
        $this->session('register_email', $email);
        $this->session('register_hash', $hash);
    }

    public function logoutUser()
    {
        // Remove user in session and cookie
        $this->session('user_id', null);
        $this->session('user_name', null);
        $this->cookie('id', 0);
        $this->cookie('signature', 0);
    }

    // Remember user in cookie for the next login
    public function rememberUser($id, $signature)
    {
        $this->cookie('id', $id);
        $this->cookie('signature', $signature);
    }

    // Save user to session
    public function saveUser($id, $name)
    {
        $this->session('user_id', $id);
        $this->session('user_name', $name);
    }

    public function createAccount()
    {
        $name = $this->session('register_name');
        $email = $this->session('register_email');
        $hash = $this->session('register_hash');

        $password = $this->process->encodePassword($email, $hash);

        $this->non($this->insert()
                        ->into('user')
                        ->values(array(
                            'name'     => $name,
                            'email'    => $email,
                            'password' => $password,
                            'register' => time()
                        ))
        );

        $user = $this->one($this->select()
                                ->from('user')
                                ->columns(array(
                                    'id',
                                    'name'
                                ))
                                ->where(array(
                                    'email' => $email
                                ))
        );

        $this->saveUser($user->id, $user->name);
    }

    public function saveInformation($question, $answer, $language, $country)
    {
        $this->non($this->update()
                        ->table('user')
                        ->set(array(
                            'active'      => 1,
                            'nationality' => $country,
                            'language'    => $language,
                            'question'    => $question,
                            'answer'      => $answer
                        ))
                        ->where(array(
                            'id' => $this->session('user_id')
                        ))
        );
    }

    public function checkActive($username)
    {
        $information = $this->one($this->select()
                                       ->from('user')
                                       ->columns(array(
                                           'active'
                                       ))
                                       ->where(array(
                                           'email' => $username
                                       ))
        );
        if ($information['active'] == 0)
            return false;
        return true;
    }
}